package com.gemme.gemme.wode;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.gemme.gemme.BaseActivity;
import com.gemme.gemme.R;
import com.gemme.gemme.login.Constant;
import com.gemme.gemme.login.MsgBean;
import com.gemme.gemme.manager.DataManager;
import com.gemme.gemme.manager.DataPares;
import com.gemme.gemme.util.ShouQuanUtil;
import com.gemme.gemme.util.StrUtil;
import com.gemme.gemme.util.asrsa.RsaNewUtils;
import com.gemme.gemme.view.OnChangeLIstener;
import com.gemme.gemme.view.RoundCheckBox;
import com.gemme.gemme.view.Topview;
import com.google.android.material.tabs.TabLayout;
import com.jaeger.library.StatusBarUtil;
import com.yanzhenjie.recyclerview.SwipeRecyclerView;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.PublicKey;
import java.util.ArrayList;
import java.util.HashMap;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

//侧滑删除地址
public class TixianActivity extends BaseActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tixian);
        initTop();
        initview();
        getIntentData();
    }
    String asserts;
    private void getIntentData() {
        try {

            asserts = getIntent().getStringExtra("data");
            if(TextUtils.isEmpty(asserts))
                asserts = "0";
            tv_assert1.setText(asserts + "");
//            tv_assert2.setText("¥" + asserts);
            tv_assert3.setText("可提现额度为"+asserts+"元");
        }catch (Exception e){

        }
    }

    RoundCheckBox rb_weixin;
    RoundCheckBox rb_ali;
    TextView tv_tixian;
    boolean isWeixin = true;

    TextView tv_assert1;
    EditText tv_assert2;
    TextView tv_assert3;
    private void initview() {
        rb_weixin = findViewById(R.id.rb_weixin);
        rb_ali = findViewById(R.id.rb_ali);
        tv_tixian = findViewById(R.id.tv_tixian);

        tv_assert1 = findViewById(R.id.tv_assert1);
        tv_assert2 = findViewById(R.id.tv_assert2);
        tv_assert3 = findViewById(R.id.tv_assert3);

        controlRb();
        rb_weixin.setChecked(true);

        tv_tixian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                startActivity(new Intent(mContext,TixianSucActivity.class));
                if(StrUtil.toLong(asserts)  <= 0){
                    Toast.makeText(mContext, "没有提现额度", Toast.LENGTH_SHORT).show();
                    return;
                }
                String price_str = tv_assert2.getText() + "";
                double price = 0;
                try {

                    price = Double.parseDouble(price_str);
                }catch (Exception e){

                }

                if(price <= 0){
                    Toast.makeText(mContext, "请输入价格", Toast.LENGTH_SHORT).show();                    return;
                }


                detailShoquan();



            }
        });
    }

    private void detailShoquan() {
        if(isWeixin){
            ShouQuanUtil.getWeiXinOpenId(mContext, new ShouQuanUtil.OnShouquan() {
                @Override
                public void onShouquan(String shouquan, String type) {
                    bindToserver(shouquan,type);
                }
            });
        }else{
            ShouQuanUtil.alipay(mContext, new ShouQuanUtil.OnShouquan() {
                @Override
                public void onShouquan(String shouquan, String type) {
                    System.out.println("");
                    bindToserver(shouquan,type);
                }
            });

        }

    }

    private void bindToserver(String shouquan, String type) {
        if(TextUtils.isEmpty(shouquan)){
            Toast.makeText(mContext, "提现失败", Toast.LENGTH_SHORT).show();
        }

        if(TextUtils.isEmpty(type)){
            Toast.makeText(mContext, "提现失败", Toast.LENGTH_SHORT).show();
        }

        String url = DataManager.BaseUrl + "user/capital/binding?" +
                "account=" + shouquan+
                "type="+type;

        HashMap<String, String> headHashMap = new HashMap<>();
        headHashMap.put(DataManager.AuthorizationKey,DataManager.getToken(this));
        DataManager.toServer2(this, url, DataManager.POST_TYPE, "", headHashMap, new DataManager.GeResponse(){

            @Override
            public void onResponse(String response) {

                try {
                    if(DataPares.isSucess(mContext,response)){
                        Toast.makeText(mContext, "授权成功", Toast.LENGTH_SHORT).show();
                        //授权成功才进入填写密码页面
                        startActivityForResult(new Intent(mContext,AddAnquanLingpaiActivity.class),1000);

                    }else{
                        Toast.makeText(mContext, "授权失败", Toast.LENGTH_SHORT).show();

                    }

                }catch (Exception e){

                }
            }
        });

    }

    private void controlRb() {
        rb_weixin.setOnChangeLIstener(new OnChangeLIstener() {
            @Override
            public void onChangeLIstener() {
                if(rb_weixin.isChecked()){
                    rb_ali.setChecked(false);
                    isWeixin = true;
                }
            }
        });
        rb_ali.setOnChangeLIstener(new OnChangeLIstener() {
            @Override
            public void onChangeLIstener() {
                if(rb_ali.isChecked()){
                    rb_weixin.setChecked(false);
                    isWeixin = false;
                }
            }
        });
    }


    private void initTop() {
//        StatusBarUtil.setColor(this,getResources().getColor(R.color.white_color) );
        StatusBarUtil.setTransparent(this);
//        StatusBarUtil.setTranslucent(this,250);
        Topview topview = findViewById(R.id.topview);
        topview.setContentText("提现");
    }

    String aqlp;
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == 1000 && data != null){

            aqlp = data.getStringExtra("aqlp");
            msg = data.getStringExtra("msg");
//            getMsg();
            toServer();
        }
    }
    String msg;
/*
    private void getMsg() {
        String url = DataManager.BaseUrl + ":9999/user/userapp/appexpassword?smsType=" + "withdrawal";
//        String url = DataManager.BaseUrl + "9999/user/userapp/appexpassword?" + "smsType="+"withdrawal"
                ;
        HashMap<String, String> headHashMap = new HashMap<>();
        headHashMap.put(DataManager.AuthorizationKey,DataManager.getToken(this));
        DataManager.toServer2(this, url, DataManager.POST_TYPE, "", headHashMap, new DataManager.GeResponse(){

            @Override
            public void onResponse(String response) {

                try {
                    if(DataPares.isSucess(mContext,response)){
                        System.out.println("");
                        MsgBean msgBean = JSON.parseObject(response, MsgBean.class);
                        msg = msgBean.getMsg();
                        double msg_d = Double.parseDouble(msg);
                        toServer();
                    }else{
                    }

                }catch (Exception e){
                    Toast.makeText(mContext, "失败了请重试", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
*/

    private void toServer() {
        String price = tv_assert2.getText() + "";
        if( TextUtils.isEmpty(price)){
            price = "0";
            return;
        }



         String aqlpRSa = testRsa(aqlp);
        if( TextUtils.isEmpty(aqlpRSa)){
            return;
        }

//        aqlpRSa = "F2vchLvXLw4acs9GaNBLk13DsVtXgbIz+E6PlzLbzxCSt5yqK8oISm+NlFvbytBLvQh0ECkTNxbo24dMhKJWum59+iiX0hapzSbP2VZQCRNJyb4VJQKVFZZ6mGQJHi64cajEM7AlJZ4oBwtxlKlHHmYRaB/A4Y/awQFjgrQDPoQ=";
//         aqlpRSa = aqlpRSa.toString().replace("+", "%2B");

        if( TextUtils.isEmpty(msg)){
            return;
        }



        String withdrawalType = isWeixin ? "2" : "1";
/*
        String url = DataManager.BaseUrl + ":9999/user/capital/withdrawal"
                ;
*/

        String url = DataManager.BaseUrl + "user/capital/withdrawal?" +
                "price=" + price +
                "&exPassword=" + aqlpRSa +
                "&cmsCode=" + msg +
                "&withdrawalType=" + withdrawalType

                ;

        HashMap<String, String> headHashMap = new HashMap<>();
        headHashMap.put(DataManager.AuthorizationKey,DataManager.getToken(this));
        DataManager.toServer2(this, url, DataManager.POST_TYPE, "", headHashMap, new DataManager.GeResponse(){

            @Override
            public void onResponse(String response) {

                try {
                    if(DataPares.isSucess(mContext,response)){
                        Toast.makeText(mContext, "提现成功", Toast.LENGTH_SHORT).show();

                    }else{
                        Toast.makeText(mContext, "提现失败", Toast.LENGTH_SHORT).show();
                    }

                    finish();

                }catch (Exception e){

                }
            }
        });
    }


    private String testRsa(String aqlp) {
        String str_rsa = "";
        try {
            if(TextUtils.isEmpty(aqlp))
                return str_rsa;
            PublicKey publicKey = RsaNewUtils.loadPublicKey(Constant.PUBLICKEY);
//            PrivateKey privateKey = RsaNewUtils.loadPrivateKey(Constant.privateKey);
//            String str = "hello";
            String publicRsa = RsaNewUtils.encryptDataStr(aqlp.getBytes("utf-8"), publicKey);
            str_rsa = publicRsa;
//            byte[] bytes = RsaNewUtils.decryptData(publicRsa.getBytes("utf-8"), privateKey);
//            String privateRsa = new String(bytes);
            System.out.println();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return str_rsa;
    }

}